<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/CountriesDomain.php';
class CountriesDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, CountriesDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO countries(shortCode,longCode,name,dialing_code)
					VALUES	(
								'".$obj->getShortCode()."',
								'".$obj->getLongCode()."',
								'".$obj->getName()."',
								".$obj->getDialing_code()."
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM countries WHERE shortCode=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE countries
				SET
					countries.longCode = '".$obj->getLongCode()."',
					countries.name = '".$obj->getName()."',
					countries.dialing_code = ".$obj->getDialing_code()."
				 WHERE shortCode=".$obj->getShortCode();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$countriesDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM countries WHERE countries.shortCode = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$countriesDomain = new CountriesDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $countriesDomain;
	}

	public function findAll()
	{
		$countriesDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM countries";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$countriesDomain = new CountriesDomain($data);
				$countriesDomainArr[] = $countriesDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $countriesDomain;
	}

}
?>